<template>
  <div class="dashboard-container">
    <!-- 统计卡片 -->
    <div class="dashboard-grid">
      <div class="stat-card">
        <div class="card-title">总资产 (USDT)</div>
        <div class="stat-value">32,458.67</div>
        <div class="stat-change negative">
          <i class="el-icon-caret-bottom"></i>
          2.45%
        </div>
      </div>
      <div class="stat-card">
        <div class="card-title">总收益率</div>
        <div class="stat-value">18.32%</div>
        <div class="stat-change positive">
          <i class="el-icon-caret-top"></i>
          2.23% 本月
        </div>
      </div>
      <div class="stat-card">
        <div class="card-title">活跃策略</div>
        <div class="stat-value">7</div>
        <div class="stat-change">
          总共 10 个策略
        </div>
      </div>
      <div class="stat-card">
        <div class="card-title">今日交易量</div>
        <div class="stat-value">128</div>
        <div class="stat-change negative">
          <i class="el-icon-caret-bottom"></i>
          12.3% 较昨日
        </div>
      </div>
    </div>

    <!-- 图表区域 -->
    <div class="dashboard-charts">
      <div class="chart-card">
        <div class="chart-header">
          <div class="chart-title">账户资产走势</div>
          <div class="chart-filters">
            <el-select v-model="timeRange" size="small" placeholder="选择时间范围">
              <el-option label="过去30天" value="30"></el-option>
              <el-option label="过去7天" value="7"></el-option>
              <el-option label="今日" value="1"></el-option>
            </el-select>
          </div>
        </div>
        <div class="chart-container" ref="assetTrendChart"></div>
      </div>
      <div class="chart-card">
        <div class="chart-header">
          <div class="chart-title">收益分布</div>
          <div class="chart-filters">
            <el-radio-group v-model="distributionType" size="small">
              <el-radio-button label="pair">交易对</el-radio-button>
              <el-radio-button label="strategy">策略</el-radio-button>
              <el-radio-button label="exchange">交易所</el-radio-button>
            </el-radio-group>
          </div>
        </div>
        <div class="chart-container" ref="assetDistributionChart"></div>
      </div>
    </div>

    <div class="dashboard-bottom">
      <!-- 活跃策略 -->
      <div class="card active-strategies">
        <div class="card-header">
          <div class="card-title">活跃策略</div>
          <div class="card-actions">
            <router-link to="/strategy/list" class="view-all">查看全部</router-link>
          </div>
        </div>
        <div class="strategy-list">
          <div v-for="(strategy, index) in activeStrategies" :key="index" class="strategy-item">
            <div class="strategy-icon" :class="strategy.iconClass">
              <i :class="strategy.icon"></i>
            </div>
            <div class="strategy-info">
              <div class="strategy-name">{{ strategy.name }}</div>
              <div class="strategy-desc">{{ strategy.exchange }} · {{ strategy.pair }}</div>
            </div>
            <div class="strategy-status">
              <el-tag :type="strategy.status === 'running' ? 'success' : 'info'" size="small" effect="plain">
                {{ strategy.status === 'running' ? '运行中' : '已停止' }}
              </el-tag>
            </div>
          </div>
        </div>
      </div>

      <!-- 主要资产 -->
      <div class="card main-assets">
        <div class="card-header">
          <div class="card-title">主要资产</div>
          <div class="card-actions">
            <router-link to="/asset/list" class="view-all">查看全部</router-link>
          </div>
        </div>
        <el-table :data="mainAssets" style="width: 100%">
          <el-table-column prop="asset" label="资产" width="180"></el-table-column>
          <el-table-column prop="amount" label="持仓量"></el-table-column>
          <el-table-column prop="value" label="当前价值"></el-table-column>
          <el-table-column prop="change" label="24h涨跌">
            <template slot-scope="scope">
              <span :class="scope.row.change >= 0 ? 'positive' : 'negative'">
                {{ scope.row.change }}%
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'Dashboard',
  data() {
    return {
      timeRange: '30',
      distributionType: 'pair',
      mainAssets: [
        { asset: 'BTC', amount: '0.5634', value: '$39,724.15', change: -2.45 },
        { asset: 'ETH', amount: '8.2341', value: '$2,167.94', change: -3.12 },
        { asset: 'SOL', amount: '85.6321', value: '$42.18', change: -1.23 },
        { asset: 'USDT', amount: '12,458.78', value: '$1.00', change: 0.00 },
        { asset: 'XRP', amount: '2,500.00', value: '$0.5236', change: -0.89 }
      ],
      activeStrategies: [
        {
          name: '三角套利',
          exchange: 'Binance OKX',
          pair: 'BTC/ETH/USDT',
          status: 'running',
          icon: 'el-icon-share',
          iconClass: 'icon-triangle'
        },
        {
          name: 'ETH网格',
          exchange: 'Binance',
          pair: 'ETH/USDT',
          status: 'running',
          icon: 'el-icon-menu',
          iconClass: 'icon-grid'
        },
        {
          name: 'BTC做市',
          exchange: 'OKX',
          pair: 'BTC/USDT',
          status: 'running',
          icon: 'el-icon-money',
          iconClass: 'icon-market'
        },
        {
          name: '统计套利',
          exchange: 'Binance OKX',
          pair: 'SOL/USDT',
          status: 'stopped',
          icon: 'el-icon-data-line',
          iconClass: 'icon-stat'
        },
        {
          name: '趋势跟踪',
          exchange: 'Binance',
          pair: 'BNB/USDT',
          status: 'running',
          icon: 'el-icon-trend-charts',
          iconClass: 'icon-trend'
        }
      ]
    }
  },
  watch: {
    distributionType: {
      handler(newType) {
        this.updateDistributionChart(newType)
      }
    }
  },
  mounted() {
    this.initAssetTrendChart()
    this.initAssetDistributionChart()
  },
  methods: {
    initAssetTrendChart() {
      const chart = echarts.init(this.$refs.assetTrendChart)
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line',
            lineStyle: {
              color: '#ddd'
            }
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月'],
          axisLine: {
            lineStyle: {
              color: '#ddd'
            }
          }
        },
        yAxis: {
          type: 'value',
          axisLine: {
            lineStyle: {
              color: '#ddd'
            }
          },
          splitLine: {
            lineStyle: {
              color: '#eee'
            }
          }
        },
        series: [{
          name: '总资产',
          type: 'line',
          smooth: true,
          data: [20000, 22000, 21500, 25000, 23000, 28000, 32000],
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              offset: 0,
              color: 'rgba(58,132,255,0.3)'
            }, {
              offset: 1,
              color: 'rgba(58,132,255,0.1)'
            }])
          },
          itemStyle: {
            color: '#3a84ff'
          },
          lineStyle: {
            color: '#3a84ff',
            width: 2
          }
        }]
      }
      chart.setOption(option)
      window.addEventListener('resize', () => chart.resize())
    },
    initAssetDistributionChart() {
      this.updateDistributionChart(this.distributionType)
    },
    updateDistributionChart(type) {
      const chart = echarts.init(this.$refs.assetDistributionChart)
      const data = this.getDistributionData(type)
      const option = {
        tooltip: {
          trigger: 'item',
          formatter: '{b}: {d}%'
        },
        legend: {
          orient: 'vertical',
          right: 10,
          top: 'center',
          data: data.map(item => item.name)
        },
        series: [{
          name: '收益分布',
          type: 'pie',
          radius: ['50%', '70%'],
          center: ['40%', '50%'],
          avoidLabelOverlap: false,
          label: {
            show: false
          },
          emphasis: {
            label: {
              show: true,
              fontSize: '14',
              fontWeight: 'bold'
            }
          },
          labelLine: {
            show: false
          },
          data: data
        }]
      }
      chart.setOption(option)
    },
    getDistributionData(type) {
      const dataMap = {
        pair: [
          { value: 55, name: 'BTC-USDT' },
          { value: 30, name: 'ETH-USDT' },
          { value: 10, name: 'SOL-USDT' },
          { value: 5, name: '其他' }
        ],
        strategy: [
          { value: 40, name: '网格交易' },
          { value: 30, name: '马丁策略' },
          { value: 20, name: '趋势跟踪' },
          { value: 10, name: '三角套利' }
        ],
        exchange: [
          { value: 45, name: 'Binance' },
          { value: 25, name: 'OKX' },
          { value: 20, name: 'Huobi' },
          { value: 10, name: '其他' }
        ]
      }
      return dataMap[type]
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;

  .dashboard-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    margin-bottom: 20px;
  }

  .stat-card {
    background-color: white;
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);

    .card-title {
      color: #909399;
      font-size: 14px;
      margin-bottom: 12px;
    }

    .stat-value {
      font-size: 26px;
      font-weight: 600;
      margin-bottom: 8px;
      color: #303133;
    }

    .stat-change {
      display: flex;
      align-items: center;
      font-size: 13px;
      
      i {
        margin-right: 4px;
      }
    }
  }

  .dashboard-charts {
    display: grid;
    grid-template-columns: 2fr 1fr;
    gap: 20px;
    margin-bottom: 20px;
  }

  .chart-card {
    background-color: white;
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);

    .chart-container {
      height: 300px;
    }
  }

  .chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
  }

  .chart-title {
    font-size: 16px;
    font-weight: 600;
    color: #303133;
  }

  .dashboard-bottom {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 20px;
  }

  .chart-filters {
    .el-radio-group {
      margin-left: 16px;
    }
  }

  .view-all {
    color: #409EFF;
    font-size: 14px;
    text-decoration: none;
    
    &:hover {
      text-decoration: underline;
    }
  }

  .card {
    background-color: white;
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
  }

  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
  }

  .card-title {
    font-size: 16px;
    font-weight: 600;
    color: #303133;
  }

  .positive {
    color: #67C23A;
  }

  .negative {
    color: #F56C6C;
  }

  ::v-deep {
    .el-radio-button__inner {
      padding: 8px 15px;
    }

    .el-tag {
      height: 24px;
      line-height: 22px;
      padding: 0 8px;
    }

    .el-table {
      th {
        background-color: #f5f7fa;
        color: #606266;
        font-weight: 500;
      }
      
      td {
        color: #606266;
      }
    }
  }

  .active-strategies {
    .card-actions {
      display: flex;
      align-items: center;
      gap: 16px;

      .view-all {
        color: #409EFF;
        font-size: 14px;
        text-decoration: none;
        
        &:hover {
          text-decoration: underline;
        }
      }
    }

    .strategy-list {
      .strategy-item {
        display: flex;
        align-items: center;
        padding: 16px 0;
        border-bottom: 1px solid #EBEEF5;

        &:last-child {
          border-bottom: none;
        }

        .strategy-icon {
          width: 40px;
          height: 40px;
          margin-right: 12px;
          border-radius: 8px;
          overflow: hidden;
          display: flex;
          align-items: center;
          justify-content: center;
          
          i {
            font-size: 20px;
            color: #fff;
          }

          &.icon-triangle {
            background: linear-gradient(135deg, #409EFF, #3a8ee6);
          }

          &.icon-grid {
            background: linear-gradient(135deg, #67C23A, #529b2e);
          }

          &.icon-market {
            background: linear-gradient(135deg, #E6A23C, #b88230);
          }

          &.icon-stat {
            background: linear-gradient(135deg, #F56C6C, #c45656);
          }

          &.icon-trend {
            background: linear-gradient(135deg, #909399, #73767a);
          }
        }

        .strategy-info {
          flex: 1;

          .strategy-name {
            font-size: 14px;
            color: #303133;
            margin-bottom: 4px;
            font-weight: 500;
          }

          .strategy-desc {
            font-size: 12px;
            color: #909399;
          }
        }

        .strategy-status {
          ::v-deep .el-tag {
            border-radius: 4px;
            padding: 0 8px;
            height: 24px;
            line-height: 22px;
            font-size: 12px;
            border: none;

            &.el-tag--success {
              color: #67C23A;
              background-color: rgba(103, 194, 58, 0.1);
            }

            &.el-tag--info {
              color: #909399;
              background-color: rgba(144, 147, 153, 0.1);
            }
          }
        }
      }
    }
  }
}
</style> 